package chart;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Millisecond;
import org.jfree.ui.TextAnchor;

import util.Constants;

import chart.ParetoChartPanel.LabelGenerator;
import db.DatabaseManager;
import db.entity.PadLife;
import db.entity.PadeLapse;
import db.entity.WCSDataInterface;

public class QuerySPCChartPanel extends ChartPanel  {
	
	private static final long serialVersionUID = 1L;

	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	private CategoryPlot querySPCPlot = null;
	private JFreeChart querySPCChart = null;
	
	public QuerySPCChartPanel()
	{
		super(null);
		
		querySPCChart = ChartFactory.createBarChart("Pad Life", "Pad", "Pad Life (Hours)", dataset, PlotOrientation.VERTICAL, false, true, false);
		querySPCChart.setBackgroundPaint(Color.white);
		
		querySPCPlot = querySPCChart.getCategoryPlot();
		querySPCPlot.setBackgroundPaint(Color.lightGray);
		querySPCPlot.setDomainGridlinePaint(Color.white);
		querySPCPlot.setRangeGridlinePaint(Color.white);
		
		NumberAxis rangeAxis = (NumberAxis)querySPCPlot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		BarRenderer paretoRenderer = (BarRenderer)querySPCPlot.getRenderer();
		paretoRenderer.setDrawBarOutline(false);
		paretoRenderer.setItemMargin(0.1D);
		
		GradientPaint gp0 = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, Color.lightGray);
		GradientPaint gp1 = new GradientPaint(0.0F, 0.0F, Constants.DARK_GREEN, 0.0F, 0.0F, Color.lightGray);
		GradientPaint gp2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, Color.lightGray);

		paretoRenderer.setSeriesPaint(0, gp0);
		paretoRenderer.setSeriesPaint(1, gp1);
		paretoRenderer.setSeriesPaint(2, gp2);

		paretoRenderer.setBaseItemLabelGenerator(new LabelGenerator());
		paretoRenderer.setBaseItemLabelsVisible(true);
		ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, -1.570796326794897D);
	
		paretoRenderer.setBasePositiveItemLabelPosition(p);
		
		ItemLabelPosition p2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, -1.570796326794897D);
		
		paretoRenderer.setPositiveItemLabelPositionFallback(p2);

		this.setChart(querySPCChart);
		this.setDisplayToolTips(true);
	}
	
	private static DatabaseManager db = null;
	public void loadData(String title, List<PadLife> pList)
	{
		dataset.clear();
		
		if (db == null)
			db = new DatabaseManager();
		for(PadLife padLifeItem : pList){
			// Calculation to get the data to plot the graph
			String padID = padLifeItem.getId().getStartLife()+""+ padLifeItem.getId().getStationName();
			List<PadeLapse> peList = db.listPadElapseOfPadID(padID);
			long elapse = 0L;
			
			for(PadeLapse peItem : peList)
				elapse += peItem.getElapseTimeMillis();
				
			dataset.setValue(elapse/3600000.0D, peList.size()+"", padLifeItem.getId().getStartLife()+"");
		}

		querySPCChart.setTitle(title);
		this.restoreAutoBounds();
	}

	static class LabelGenerator extends StandardCategoryItemLabelGenerator
	{
		private static final long serialVersionUID = 1L;
		public String generateLabel(CategoryDataset dataset, int series, int category)
		{
			return Constants.GRAPH_DF.format(dataset.getValue(series, category));
		}
	}
	
	public void drawChart(Graphics2D g2, Rectangle2D r2D)
	{
		querySPCChart.draw(g2, r2D);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) {
		db.DatabaseManager dm = new db.DatabaseManager();
		DateFormat dtFmt = new java.text.SimpleDateFormat();
		
		List sList;
		try {
			sList = dm.listPadlifeByStationNameDate("01", dtFmt.parse("12/10/2010 0:0 PM, PDT"), dtFmt.parse("12/20/2010 21:00 PM, PDT"));
			System.out.println("List: "+sList.size());
			QuerySPCChartPanel spcPnl = new QuerySPCChartPanel();
			spcPnl.loadData("Station 01", sList);
			javax.swing.JFrame window = new javax.swing.JFrame("Testing Graph");
			window.setLayout(new FlowLayout());
			window.getContentPane().add(spcPnl);
	        window.setSize(800, 500);
	        window.setLocation(50, 50);
			window.setVisible(true);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
